//
//  NSDictionary+LSCore.h
//  zeekol
//
//  Created by gaojiefeng on 2020/7/15.
//  Copyright © 2020 gaojiefeng. All rights reserved.
//
#import "NSObject+LSCore.h"

@implementation NSObject (LSCore)


- (BOOL)isStringValid {
    
    if (self && [self isKindOfClass:[NSString class]]) {
        return YES;
    }
    else {
        return NO;
    }
}

- (BOOL)isDictionaryValid {
    
    if (self && [self isKindOfClass:[NSDictionary class]]) {
        if ([[((NSDictionary *)self) allKeys] count] > 0) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)isArrayValid {
    
    if (self && [self isKindOfClass:[NSArray class]]) {
        return YES;
    }
    else {
        return NO;
    }
}


/**
 网络请求是否成功
 */
- (BOOL)isRequestSuccess {
    NSDictionary *dic = (NSDictionary *)self;
    // 0：成功；2：错误; 1和1000 - token失效
    if (dic && [dic isKindOfClass:[NSDictionary class]]) {
        NSString *state = dic[@"code"];
        if ([state isStringValid] && [state isEqualToString:@"0"]) {
            return YES;
        }
    }
    return NO;
}

/**
 获取请求的错误信息errmsg
 */
- (NSString *)getRequestErrMsg {
    NSDictionary *dic = (NSDictionary *)self;
    if (dic && [dic isKindOfClass:[NSDictionary class]]) {
        NSString *errMsg = dic[@"msg"];
        if ([errMsg isStringValid]) {
            return errMsg;
        }
    }
    return tmsRequestFailedString;
}

// 获取请求的data
- (id)getRequestData {
    NSDictionary *dic = (NSDictionary *)self;
    if (dic && [dic isKindOfClass:[NSDictionary class]]) {
        id data = dic[@"data"];
        if (data) {
            return data;
        }
    }
    return nil;
}


- (BOOL)isIPhoneX {
    // 根据安全区域判断
    if (@available(iOS 11.0, *)) {
        CGFloat height = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
        return (height > 0);
    } else {
        return NO;
    }
}

@end
